package com.ys.controller.api.outer;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.ys.annotation.Log;
import com.ys.entity.hr.ThrOperateCompanyApply;
import com.ys.entity.storage.TccAllocateDetail;
import com.ys.entity.storage.TccDistribute;
import com.ys.entity.storage.TccDistributeDetail;
import com.ys.entity.sys.TsOrganizationInfo;
import com.ys.entity.sys.TsUserInfo;
import com.ys.enume.log.BusinessTypeEnum;
import com.ys.service.amazonorder.TcoDayAllService;
import com.ys.service.amazonorder.TcoDayCountryService;
import com.ys.service.amazonorder.TcoDayProductJPEUROPEService;
import com.ys.service.amazonorder.TcoDayProductJPNORTHService;
import com.ys.service.amazonorder.TcoDayProductJPService;
import com.ys.service.amazonorder.TcoDayRegionService;
import com.ys.service.amazonorder.TcoDayShopService;
import com.ys.service.amazonorder.TcoMonthAllService;
import com.ys.service.amazonorder.TcoMonthCountryService;
import com.ys.service.amazonorder.TcoMonthProductJPEUROPEService;
import com.ys.service.amazonorder.TcoMonthProductJPNORTHService;
import com.ys.service.amazonorder.TcoMonthProductJPService;
import com.ys.service.amazonorder.TcoMonthProductJPSumService;
import com.ys.service.amazonorder.TcoMonthProductService;
import com.ys.service.amazonorder.TcoMonthProductTypeService;
import com.ys.service.amazonorder.TcoMonthProductYPService;
import com.ys.service.amazonorder.TcoMonthProductYPSumService;
import com.ys.service.amazonorder.TcoMonthRegionService;
import com.ys.service.amazonorder.TcoMonthShopService;
import com.ys.service.amazonorder.TcoYearAllService;
import com.ys.service.amazonorder.TcoYearCountryService;
import com.ys.service.amazonorder.TcoYearProductJPEUROPEService;
import com.ys.service.amazonorder.TcoYearProductJPNORTHService;
import com.ys.service.amazonorder.TcoYearProductJPService;
import com.ys.service.amazonorder.TcoYearProductJPSumService;
import com.ys.service.amazonorder.TcoYearProductService;
import com.ys.service.amazonorder.TcoYearProductTypeService;
import com.ys.service.amazonorder.TcoYearProductYPService;
import com.ys.service.amazonorder.TcoYearProductYPSumService;
import com.ys.service.amazonorder.TcoYearRegionService;
import com.ys.service.amazonorder.TcoYearShopService;
import com.ys.service.amazonorder.TpAmazonShopOrderDailyService;
import com.ys.service.amazonorder.TpAmazonShopOrderService;
import com.ys.service.business.TcwAccountDetailedForCompanyService;
import com.ys.service.flow.YspTaskService;
import com.ys.service.hr.ThrOperateCompanyApplyService;
import com.ys.service.sys.TsOrganizationInfoService;
import com.ys.util.DateUtil;
import com.ys.util.shiro.ShiroUtils;

@Controller
@RequestMapping("/api/outer/demo")
public class ApiOuterDemoController {
	@Autowired
	private ThrOperateCompanyApplyService thrOperateCompanyApplyService;
	@Autowired
	private TpAmazonShopOrderService tpAmazonShopOrderService;

	@Autowired
	private TpAmazonShopOrderDailyService tpAmazonShopOrderDailyService;

	@Autowired
	private TsOrganizationInfoService tsOrganizationInfoService;

	@Autowired
	private TcwAccountDetailedForCompanyService tcwAccountDetailedForCompanyService;
	@Autowired
	private YspTaskService yspTaskService;
	@Autowired
	private TcoYearAllService tcoYearAllService;
	@Autowired
	private TcoYearCountryService tcoYearCountryService;
	@Autowired
	private TcoYearRegionService tcoYearRegionService;
	@Autowired
	private TcoYearProductService tcoYearProductService;
	@Autowired
	private TcoYearProductYPService tcoYearProductYPService;
	@Autowired
	private TcoYearProductJPService tcoYearProductJPService;
	@Autowired
	private TcoYearProductYPSumService tcoYearProductYPSumService;
	@Autowired
	private TcoYearProductJPSumService tcoYearProductJPSumService;
	@Autowired
	private TcoYearProductJPNORTHService tcoYearProductJPNORTHService;
	@Autowired
	private TcoYearProductJPEUROPEService tcoYearProductJPEUROPEService;
	@Autowired
	private TcoYearProductTypeService tcoYearProductTypeService;
	@Autowired
	private TcoMonthAllService tcoMonthAllService;
	@Autowired
	private TcoMonthCountryService tcoMonthCountryService;
	@Autowired
	private TcoMonthShopService tcoMonthShopService;
	@Autowired
	private TcoMonthRegionService tcoMonthRegionService;
	@Autowired
	private TcoDayShopService tcoDayShopService;
	@Autowired
	private TcoYearShopService tcoYearShopService;
	@Autowired
	private TcoMonthProductService tcoMonthProductService;
	@Autowired
	private TcoMonthProductYPService tcoMonthProductYPService;
	@Autowired
	private TcoMonthProductJPService tcoMonthProductJPService;
	@Autowired
	private TcoMonthProductYPSumService tcoMonthProductYPSumService;
	@Autowired
	private TcoMonthProductJPSumService tcoMonthProductJPSumService;
	@Autowired
	private TcoMonthProductJPNORTHService tcoMonthProductJPNORTHService;
	@Autowired
	private TcoMonthProductJPEUROPEService tcoMonthProductJPEUROPEService;
	@Autowired
	private TcoMonthProductTypeService tcoMonthProductTypeService;
	@Autowired
	private TcoDayAllService tcoDayAllService;
	@Autowired
	private TcoDayCountryService tcoDayCountryService;
	@Autowired
	private TcoDayRegionService tcoDayRegionService;

	@Autowired
	private TcoDayProductJPService tcoDayProductJPService;
	@Autowired
	private TcoDayProductJPNORTHService tcoDayProductJPNORTHService;
	@Autowired
	private TcoDayProductJPEUROPEService tcoDayProductJPEUROPEService;

	@RequestMapping("/test")
	@ResponseBody
	public void test(HttpServletRequest request, HttpServletResponse response, String dayBegin) {

		List<TccDistributeDetail> listTccDistributeDetail = new TccDistributeDetail()
				.addLeftJoin("TCC_Distribute B on B.Bid = A.BidDistribute")
				.where("B.EffectStock = 1")
				.queryList();
		Date now = new Date();
		for(TccDistributeDetail detail : listTccDistributeDetail) {
			TccDistribute entity = new TccDistribute().setBid(detail.getBidDistribute()).queryFirst();
			TccAllocateDetail tccAllocateDetail = new TccAllocateDetail().setId(detail.getAllocateDetailId()).queryFirst();
			
			BigDecimal amount = detail.getSinglePrice().multiply(new BigDecimal(detail.getNum()));
			String vGsBm = tccAllocateDetail.getVgsbmFrom();
			String vGsBmP = tccAllocateDetail.getVgsbmTo();
			String vJyLx = "采购服务→采购成本";
			BigDecimal fJe = amount;
			Date dJySj = now;
			Date dShSj = now;
			String vBz = "手工分配单," + entity.getBid() + "," + tccAllocateDetail.getProductCode() + "," + detail.getNum();
			String mName = null;
			Integer mType = null;
			String mNid = null;
			String mBid = null;
			String mGuid = null;
			Date writeTime = now;
			try {
				tcwAccountDetailedForCompanyService.addTcwAccountDetailedForCompany(vGsBm, vGsBmP, vJyLx, fJe, dJySj,
						dShSj, vBz,entity.getBid(), writeTime);
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		
	}

	@RequestMapping("/test3")
	@ResponseBody
	public void test3(HttpServletRequest request, HttpServletResponse response, String dayBegin) {

		List<TsUserInfo> listTsUserInfo = new TsUserInfo().queryList();
		for (TsUserInfo tsUserInfo : listTsUserInfo) {
			tsOrganizationInfoService.repairUserOrg(tsUserInfo.getUserId());
		}
	}

	@RequestMapping("/test4")
	@ResponseBody
	public void test4(HttpServletRequest request, HttpServletResponse response, String dayBegin) {

		String a = ShiroUtils.getSession().getAttribute("guidYsRole").toString();
		System.out.println(a);
	}

	@RequestMapping("/test5")
	@ResponseBody
	public void test5(HttpServletRequest request, HttpServletResponse response, String dayBegin) {
		List<TsOrganizationInfo> listTsOrganizationInfo = new TsOrganizationInfo().where("A.OrgKind != 'user'")
				.setOrderby("A.CreateTime asc")
				.queryList();
		try {
			for (TsOrganizationInfo tsOrganizationInfo : listTsOrganizationInfo) {
				tsOrganizationInfoService.repairNode(tsOrganizationInfo.getGuid());
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	@RequestMapping("/test6")
	@ResponseBody
	public void test6(HttpServletRequest request, HttpServletResponse response) {

		tcwAccountDetailedForCompanyService.updateBatch();
	}

	/*
	 * 修复节点值数据
	 * 注意：
	 * 1、顶级节点VGsBmP的值为C101的，需要提前手动设置好
	 * 2、其余floor值置为空
	 */
	@RequestMapping("/test7")
	@ResponseBody
	public void test7(HttpServletRequest request, HttpServletResponse response) {
		List<ThrOperateCompanyApply> listThrOperateCompanyApply = new ThrOperateCompanyApply()
				.where("A.Floor is null")
				.setOrderby("A.CreateTime asc")
				.where("A.Id > 1")
				.queryList();
		for (ThrOperateCompanyApply entity : listThrOperateCompanyApply) {
			String vGsBm = entity.getVgsbm();
			try {
				thrOperateCompanyApplyService.repairNode(vGsBm);
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	}

	@RequestMapping("/test8")
	@ResponseBody
	@Log(name = "测试test8", businessType = BusinessTypeEnum.UPDATE)
	public void test8(HttpServletRequest request, HttpServletResponse response) {

		Date now = new Date();
		String today = DateUtil.getYYYY_MM_DD(now);
		Date dateBegin = DateUtil.parseYYYY_MM_dd("2023-11-20");

		for (int i = 0; i < 365; i++) {
			Date dateCurrent = DateUtil.addDays(dateBegin, i);
			String dayCurrent = DateUtil.getYYYY_MM_DD(dateCurrent);

			if (dayCurrent.compareTo(today) > 0) {
				break;
			}

//			tcoDayAllService.addBatch(dayCurrent);
//
//			tcoDayCountryService.addBatch(dayCurrent);
//
//			tcoDayRegionService.addBatch(dayCurrent);
//
//			tcoDayProductJPService.addBatch(dayCurrent);
//
//			tcoDayProductJPSumService.addBatch(dayCurrent);

			tcoDayProductJPNORTHService.addBatch(dayCurrent);

			tcoDayProductJPEUROPEService.addBatch(dayCurrent);

//
		}

	}

	@RequestMapping("/test9")
	@ResponseBody
	public void test9(HttpServletRequest request, HttpServletResponse response) {

		Date now = new Date();
		String today = DateUtil.getYYYY_MM(now);
		Date dateBegin = DateUtil.parseYYYY_MM_dd("2023-11-01");

		for (int i = 0; i < 12; i++) {
			Date dateCurrent = DateUtil.addMonth(dateBegin, i);
			String dayCurrent = DateUtil.getYYYY_MM(dateCurrent);

			if (dayCurrent.compareTo(today) > 0) {
				break;
			}

//			tcoMonthAllService.addBatch(dayCurrent);
//
//			tcoMonthCountryService.addBatch(dayCurrent);
//
//			tcoMonthRegionService.addBatch(dayCurrent);
//
//			tcoMonthProductService.addBatch(dayCurrent);
//
//			tcoMonthProductYPService.addBatch(dayCurrent);
//
//			tcoMonthProductJPService.addBatch(dayCurrent);
//
//			tcoMonthProductTypeService.addBatch(dayCurrent);
//
//			tcoMonthProductYPSumService.addBatch(dayCurrent);
//
//			tcoMonthProductJPSumService.addBatch(dayCurrent);

			tcoMonthProductJPEUROPEService.addBatch(dayCurrent);

			tcoMonthProductJPNORTHService.addBatch(dayCurrent);
		}

	}

	@RequestMapping("/test10")
	@ResponseBody
	public void test10(HttpServletRequest request, HttpServletResponse response) {

		String year = "2023";

//		tcoYearAllService.addBatch(year);
//
//		tcoYearCountryService.addBatch(year);
//
//		tcoYearRegionService.addBatch(year);
//
//		tcoYearProductService.addBatch(year);
//
//		tcoYearProductYPService.addBatch(year);
//
//		tcoYearProductJPService.addBatch(year);
//
//		tcoYearProductTypeService.addBatch(year);
//
//		tcoYearProductYPSumService.addBatch(year);
//
//		tcoYearProductJPSumService.addBatch(year);

		tcoYearProductJPEUROPEService.addBatch(year);

		tcoYearProductJPNORTHService.addBatch(year);


	}
	
	@RequestMapping("/test13")
	@ResponseBody
	public void test13(HttpServletRequest request, HttpServletResponse response) {

		Date now = new Date();

		for (int i = -21; i <= 0; i++) {
			Date dateCurrent = com.bo.util.DateUtil.addDays(now, i);
			String dayCurrent = com.bo.util.DateUtil.getYYYY_MM_DD(dateCurrent);

			tcoDayCountryService.addBatch(dayCurrent);
		}


	}
}




